package huoye.shops.huoye.operation.presenter;


import android.util.Log;

import com.codeideology.android.utils.StringUtils;

import java.util.List;

import huoye.shops.base.BasePresenter;
import huoye.shops.base.MineBaseJsonRequestCallback;
import huoye.shops.common.model.OssImageData;
import huoye.shops.huoye.operation.model.ShopEventPrizeList;
import huoye.shops.huoye.operation.networt.OperationService;
import huoye.shops.huoye.operation.view.IBuildEventView;
import huoye.shops.huoye.operation.view.IEditAddShopView;
import huoye.shops.huoye.user.model.User;


/**
 * Created by uesr on 2017/7/19.
 */

public class BuildEventPresenter extends BasePresenter<IBuildEventView> {


    private OperationService mService;





    //单张图片上传
    public void uploadOssImg(String id,String type,String filePath){
        //  view.showProcessingIndicator();
            mService.uploadOssImg(id,type,filePath,new MineBaseJsonRequestCallback<OssImageData>(OssImageData.class) {

               @Override
                public void onSuccess(List<OssImageData> list) {
                    super.onSuccess(list);
                   OssImageData ossImageData = list.get(0);
                   view.setUpload(ossImageData);
                }

                //网络请求失败
                @Override
                public void onFailure(String msg, String code, Throwable e) {
                    super.onFailure(msg, code, e);
                    view.makeToast(msg);
                }

                //加载结束
                @Override
                public void onNetworkFail() {
                    super.onNetworkFail();
                    view.onNetworkFail();
                }

                //加载中
                @Override
                public void onFinish() {
                    super.onFinish();
                    view.hideProcessingIndicator();
                }
            });

    }

    //多张图片上传
    public void uploadOssImgMore(String id,String type,List<String> imgUrl){
        //  view.showProcessingIndicator();
            mService.uploadOssImgMore(id,type,imgUrl,new MineBaseJsonRequestCallback<OssImageData>(OssImageData.class) {

               @Override
                public void onSuccess(List<OssImageData> list) {
                    super.onSuccess(list);
                   view.setUploadMore(list);
                }

                //网络请求失败
                @Override
                public void onFailure(String msg, String code, Throwable e) {
                    super.onFailure(msg, code, e);
                    view.makeToast(msg);
                }

                //加载结束
                @Override
                public void onNetworkFail() {
                    super.onNetworkFail();
                    view.onNetworkFail();
                }

                //加载中
                @Override
                public void onFinish() {
                    super.onFinish();
                    view.hideProcessingIndicator();
                }
            });

    }


    //图片删除
    public void deleteOssImg(String filePath){
        //  view.showProcessingIndicator();
        mService.deleteOssImg(filePath,new MineBaseJsonRequestCallback<OssImageData>(OssImageData.class) {

            @Override
            public void onSuccess() {
                super.onSuccess();
                view.setDelImage();
            }

            //网络请求失败
            @Override
            public void onFailure(String msg, String code, Throwable e) {
                super.onFailure(msg, code, e);
                view.makeToast(msg);
            }

            //加载结束
            @Override
            public void onNetworkFail() {
                super.onNetworkFail();
                view.onNetworkFail();
            }

            //加载中
            @Override
            public void onFinish() {
                super.onFinish();
                view.hideProcessingIndicator();
            }
        });

    }



    int intLotteryNum = 0;
    int  intWinsNum = 0;
    //商家创建活动（目前只创建共享奖池）
    public void EventCreate(String eventTheme,String etImg, String remarks, String beginTime, String endTime,
                            String userLotteryNum, String mostWinsNum,
                            List<ShopEventPrizeList> lists){


        if(remarks.equals("")){
            view.makeToast("请输入活动说明");
            return;
        }else if(beginTime.equals("")){
            view.makeToast("请选择活动开始时间");
            return;
        }else if(endTime.equals("")){
            view.makeToast("请选择活动结束时间");
            return;
        }else if(Integer.valueOf(userLotteryNum) == 0 ){
            view.makeToast("请输入用户抽奖次数");
            intLotteryNum  = Integer.valueOf(userLotteryNum);
            return;
        }else if(Integer.valueOf(mostWinsNum) == 0 ){
            view.makeToast("请输入用户可中奖次数");
            intWinsNum = Integer.valueOf(mostWinsNum);
            return;
        }else if(intLotteryNum <intWinsNum ){
            view.makeToast("每人最多中奖次数 (不能大于抽奖次数)");
            return;
        }else {
            mService.EventCreate(eventTheme,etImg,remarks,beginTime,endTime,userLotteryNum,mostWinsNum,lists,new MineBaseJsonRequestCallback<User>(User.class) {
                @Override
                public void onSuccess() {
                    super.onSuccess();
                    view.makeToast("活动创建成功");
                    view.finish();
                }

                //网络请求失败
                @Override
                public void onFailure(String msg, String code, Throwable e) {
                    super.onFailure(msg, code, e);
                    view.makeToast(msg);
                }

                //加载结束
                @Override
                public void onNetworkFail() {
                    super.onNetworkFail();
                    view.onNetworkFail();
                }

                //加载中
                @Override
                public void onFinish() {
                    super.onFinish();
                    view.hideProcessingIndicator();
                }
            });
        }



    }




    @Override
    public void onStart() {
        mService = new OperationService();
    }
}
